﻿// Copyright (C) 2012 Jeff Tanner <jeff00seattle@gmail.com>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

using System;
using System.Collections.Generic;
using System.Text;

namespace WindowsAzure_StorageREST
{
    public class Blob : BlobService
    {
        /// <summary>
        /// Gets or sets the name of a blob container.
        /// </summary>
        /// <value>The name of a blob container.</value>
        public string BlobContainerName { get; set; }

        /// <summary>
        /// Gets or sets the name.
        /// </summary>
        /// <value>The name.</value>
        public string Name { get; set; }

        /// <summary>
        /// Gets or sets the URL.
        /// </summary>
        /// <value>The URL.</value>
        public Uri Url { get; set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="Blob"/> class.
        /// </summary>
        /// <param name="storageEndpoint">The storage endpoint.</param>
        public Blob(StorageEndpoint storageEndpoint)
            : base(storageEndpoint)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Blob"/> class.
        /// </summary>
        /// <param name="storageEndpoint">The storage endpoint.</param>
        /// <param name="blobContainerName">Name of a blob container.</param>
        public Blob (
            StorageEndpoint storageEndpoint,
            string blobContainerName
            )
            : base(storageEndpoint)
        {
            this.BlobContainerName = blobContainerName;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Blob"/> class.
        /// </summary>
        /// <param name="storageEndpoint">The storage endpoint.</param>
        /// <param name="blobContainerName">Name of a blob container.</param>
        /// <param name="blobName">Name of a blob.</param>
        public Blob (
            StorageEndpoint storageEndpoint,
            string blobContainerName,
            string blobName
            )
            : base(storageEndpoint)
        {
            this.BlobContainerName = blobContainerName;
            this.Name = blobName;
        }

        /// <summary>
        /// Gets a blob metadata.
        /// </summary>
        /// <param name="this.Name">Name of a blob.</param>
        /// <returns></returns>
        public BlobServiceResponse RetrieveBlobMetadata()
        {
            Dictionary<string, string> queryParams = new Dictionary<string, string>();

            queryParams.Add("comp", "metadata");
            base.InitializeRequest(String.Format("{0}/{1}", this.BlobContainerName, this.Name), queryParams);
            base.SetMethod("HEAD");

            return new BlobServiceResponse(base.DispatchRequest());
        }

        /// <summary>
        /// Puts a blob within a blob container.
        /// </summary>
        /// <param name="this.Name">Name of a blob.</param>
        /// <param name="blobContents">The Blob contents.</param>
        /// <returns></returns>
        public BlobServiceResponse UpdateBlob(byte[] blobContents)
        {
            base.InitializeRequest(String.Format("{0}/{1}", this.BlobContainerName, this.Name), null);
            base.SetMethod("PUT");
            base.SetContent(blobContents);

            return new BlobServiceResponse(base.DispatchRequest());
        }

        /// <summary>
        /// Puts pseudo folder within a blob container.
        /// </summary>
        /// <param name="this.Name">Name of a blob.</param>
        /// <param name="blobContents">The Blob contents.</param>
        /// <returns></returns>
        public BlobServiceResponse PutBlobFolder()
        {
            this.Name = Blob.BlobFolderName(this.Name);
            base.InitializeRequest(String.Format("{0}/{1}", this.BlobContainerName, this.Name), null);
            base.SetMethod("PUT");

            return new BlobServiceResponse(base.DispatchRequest());
        }

        /// <summary>
        /// Deletes a blob.
        /// </summary>
        /// <param name="this.Name">Name of a blob.</param>
        /// <param name="blobContents">The Blob contents.</param>
        /// <returns></returns>
        public BlobServiceResponse DeleteBlob()
        {
            base.InitializeRequest(String.Format("{0}/{1}", this.BlobContainerName, this.Name), null);
            base.SetMethod("DELETE");

            return new BlobServiceResponse(base.DispatchRequest());
        }

        /// <summary>
        /// Gets a blob.
        /// </summary>
        /// <returns></returns>
        public BlobServiceResponse RetrieveBlob()
        {
            base.InitializeRequest(String.Format("{0}/{1}", this.BlobContainerName, this.Name), null);
            base.SetMethod("GET");

            return new BlobServiceResponse(base.DispatchRequest());
        }

        /// <summary>
        /// Sets a blob metadata.
        /// </summary>
        /// <param name="this.Name">Name of a blob.</param>
        /// <param name="metaData">The meta data.</param>
        /// <returns></returns>
        public BlobServiceResponse UpdateBlobMetadata(Dictionary<string, string> metaData)
        {
            Dictionary<string, string> queryParams = new Dictionary<string, string>();

            queryParams.Add("comp", "metadata");
            base.InitializeRequest(String.Format("{0}/{1}", this.BlobContainerName, this.Name), queryParams);
            base.SetMethod("PUT");
            base.SetFunctionHeaders(metaData);

            return new BlobServiceResponse(base.DispatchRequest());
        }

        /// <summary>
        /// BLOBs the name of the folder.
        /// </summary>
        /// <param name="blobPath">The Blob path.</param>
        /// <returns></returns>
        public static string BlobFolderName(string blobPath)
        {
            char[] charTrim = { '\\', '/' };
            string trimmed = blobPath.TrimEnd(charTrim);
            return String.Format("{0}/$$$.$$$", trimmed);
        }
    }
}
